package com.yangran.strategy.factory;

import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略工厂
 *
 * @author lishan
 * @date 2023/12/19
 */
@Component
public class MessageHandlerFactory {

    private static Map<String, MessageHandlerStrategy> STRATEGY_MAP = new ConcurrentHashMap<>();

    public MessageHandlerFactory(Map<String, MessageHandlerStrategy> strategyMap) {
        STRATEGY_MAP.clear();
        strategyMap.forEach((k, v) -> STRATEGY_MAP.put(k, v));
    }

    // todo: 策略为空的处理 1、在生产端处理 2、在使用端处理
    public MessageHandlerStrategy getHandler(String handlerType) {
        return STRATEGY_MAP.get(handlerType);
    }
}
